<?php

class Model_users extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

//---------------------------------------------------
// start function insert_registers

    function insert_registers() {
        $myname = $_POST['myname'];
        $mysurname = $_POST['mysurname'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];

        $data = array(
            'person_id' => '',
            'name' => $myname,
            'surname' => $mysurname,
            'username' => $username,
            'password' => $password,
            'email' => $email,
            'active' => '0',
        );

        $this->db->insert('person', $data);
    }

// end function insert_registers
//---------------------------------------------------

    function select_name() {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $sql_name = "select * ";
        $sql_name .= "from person ";
        $sql_name .= "where username = '$username' and password = '$password' ";
        $sql_name .= "and active <> 0";

        $sql_name = $this->db->query($sql_name);

        if ($sql_name->num_rows() > 0) {
            $CreateSession = array(
                'username' => $username,
                'password' => $password,
                'logged_in' => TRUE
            );
        } else {
            $this->session->set_flashdata('message', '<div id="message">Oopsie, it seems your username or password is incorrect, please try again.</div>');
            redirect("con_users", "");
        }
        $this->session->set_userdata($CreateSession);
        return $sql_name->result();
    }

    function select_login() {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $sql_menu = "select c.gperson_id, cmg_id, f.gmenu_id, d.menu_id, menu_name, menu_link, position ";
        $sql_menu .= "from person a, chk_person_group b, group_person c, chk_menu_group d,  chk_menu_person e,group_menu f, menu g ";
        $sql_menu .= "where a.person_id = b.person_id
                 and  b.gperson_id = c.gperson_id 
                 and  b.gperson_id = e.gperson_id 
                 and  e.gmenu_id = d.gmenu_id 
                and  d.gmenu_id = f.gmenu_id 
                and  d.menu_id = g.menu_id
                 and a.person_id <> '' ";
        $sql_menu .= "and a.username = '$username' and a.password = '$password' ";
        $sql_menu .= "and a.active <> 0
                 and f.active <> 0
                 and g.active <> 0
                 group by  menu_id,gmenu_id
                 order by menu_id";

        $data = $this->db->query($sql_menu);

        if ($data->num_rows() > 0) {
            return $data->result();
        }
    }

    function select_person() {
        $query = $this->db->get('person');
        return $query->result();
    }

    function select_person_type() {
        if (isset($_POST['select_type']) != null) {
            $select_type = $_POST['select_type'];

            $sql = "select * from person ";
            if ($select_type != "2") {
                $sql .= "where active = " . $select_type . " ";
                $sql .= "order by person_id";
            } else {
                $sql .= "order by person_id";
            }

            $data = $this->db->query($sql);
            return $data->result();
        }
    }
    
    //---------------------------------------------------
// start function insert_person
    function insert() { 
        // sql insert person
        $personidmax = $_POST['personidmax']+1;
        $myname = $_POST['fname'];
        $mysurname = $_POST['sname'];
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $vpassword = $_POST['vpassword'];
        $active = $_POST['active'];
        $data = array(
            'person_id' => $personidmax,
            'name' => $myname,
            'surname' => $mysurname,
            'username' => $username,
            'password' => $password,
            'email' => $email,
            'active' => $active,
        );
        $this->db->insert('person', $data);
        
        // sql insert chk_person_group
        for($i=0; $i<sizeof($_POST['gperson_id']); $i++)
        {
            $gperson_id[$i] = $_POST['gperson_id'][$i];
            $data = array(
                'cpg_id' => '',
                'person_id' => $personidmax,
                'gperson_id' => $gperson_id[$i]
            );
            $this->db->insert('chk_person_group', $data);
        }  
      } 
// end function insert_person
//---------------------------------------------------
// start function update_active_users
    
    function update_active_users() {
        $person_value = $_POST['update_status_person_id'];
        $person_id = substr($person_value, 2) . "<br />";
        $active_id = substr($person_value, 0, 1);

        if ($active_id == "1") {
            $data = array(
                'active' => '0'
            );
        } else {
            $data = array(
                'active' => '1'
            );
        }

        $this->db->where('person_id', $person_id);
        $this->db->update('person', $data);
    }

// end function update_active_users
//---------------------------------------------------
// start function delete_active_users

    function delete_active_users() {
        $person_id = $_POST['delete_person'];
        
        $tables = array('person', 'chk_person_group');
        $this->db->where('person_id', $person_id);
        $this->db->delete($tables);

        
    }


//---------------------------------------------------

    
    function edit_users() {
        $personid = $_POST['personid'];
        $myname = $_POST['fname'];
        $mysurname = $_POST['sname'];
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $npassword = $_POST['npassword'];
        $vpassword = $_POST['vpassword'];
        $active = $_POST['active'];
        $data = array(
            'name' => $myname,
            'surname' => $mysurname,
            'username' => $username,
            'password' => $password,
            'email' => $email,
            'active' => $active,
        );

        $this->db->where('person_id', $person_id);
        $this->db->update('person', $data);
    }

// end function edit_users
//---------------------------------------------------

    function select_group() {
        $query = $this->db->get('group_person') ;
        return $query->result();        
    }
    
    function select_chkgroup() {
        $personid = $_POST['editperson'];
        //$query = $this->db->get('group_person') ;
        $sql = "select c.gperson_id, c.person_id 
                from group_person a, chk_person_group c 
                where a.gperson_id = c.gperson_id and c.person_id = $personid";
        $data = $this->db->query($sql);
        return $data->result();        
    }
          
    function select_personid() {
        $this->db->select_max('person_id');
        $query = $this->db->get('person');
        return $query->result();        
    }
    
    function select_edit() {
        $person_id = $_POST['editperson'];
       
        $query = $this->db->get_where('person', array('person_id' => $person_id));
        return $query->result();        
    }
//---------------------------------------------------------------------
// start group manager
    function select_gperson_type() {
        if (isset($_POST['select_type']) != null) {
            $select_type = $_POST['select_type'];

            $sql = "select * from group_person ";
            if ($select_type != "2") {
                $sql .= "where active = " . $select_type . " ";
                $sql .= "order by gperson_id";
            } else {
                $sql .= "order by gperson_id";
            }

            $data = $this->db->query($sql);
            return $data->result();
        }
    }
    
    function update_active_groupperson() {
        $gperson_value = $_POST['activegperson'];
        $gperson_id = substr($gperson_value, 2) . "<br />";
        $active_id = substr($gperson_value, 0, 1);

        if ($active_id == "1") {
            $data = array(
                'active' => '0'
            );
        } else {
            $data = array(
                'active' => '1'
            );
        }

        $this->db->where('gperson_id', $gperson_id);
        $this->db->update('group_person', $data);
    }

    function delete_groupperson() {
        $gperson_id = $_POST['deletegperson'];
        
        $tables = array('group_person', 'chk_person_group', 'chk_menu_group');
        $this->db->where('gperson_id', $gperson_id);
        $this->db->delete($tables);
        
    }
    
    function insertgroup() { 
        $groupname = $_POST['groupname'];
        $active = $_POST['active'];
        $data = array(
            'gperson_id' => '',
            'gperson_name' => $groupname,
            'active' => $active,
        );
        $this->db->insert('group_person', $data); 
     } 
     
     function select_editgroup() {
        $gperson_id = $_POST['editgperson'];
       
        $query = $this->db->get_where('group_person', array('gperson_id' => $gperson_id));
        return $query->result();  
     }
     
     function edit_groupusers() {
        $gpersonid = $_POST['gpersonid'];
        $gperson_name = $_POST['gperson_name'];
        $active = $_POST['active'];
        $data = array(
            'gperson_name' => $gperson_name,
            'active' => $active,
        );

        $this->db->where('gperson_id', $gperson_id);
        $this->db->update('group_person', $data);
    }

//---------------------------------------------------------------------
// start menu manager
     
       
    function select_menu_of_group() {
        {
        $gmenu_name = $_POST['select'];
        if($gmenu_name=="-all menu-"){
              $query = $this->db->query("select * from menu");		
               return $query->result();}
 else {
    // $gmenu= $_POST['gmenu'];
        $query = $this->db->query("select menu_name,menu_link,a.active,c.menu_id,gmenu_name from menu a,group_menu b,chk_menu_group c where c.menu_id = a.menu_id and c.gmenu_id = b.gmenu_id and b.gmenu_name = '$gmenu_name'group by menu_name");
             //$query = $this->db->query("select * from menu");		
               return $query->result();
}
      
    }
    }
    
    function select_menu_all_type() {        
        if (isset($_POST['select_menu_all_id']) != null) {
            $select_type = $_POST['select_menu_all_id'];

            $sql = "select * from menu ";
            if ($select_type != "2") {
                $sql .= "where active = " . $select_type . " ";
                $sql .= "order by menu_id";
            } else {
                $sql .= "order by menu_id";
            }

            $data = $this->db->query($sql);
            return $data->result();
        }
    }
    
    function select_menu(){
        
        $query = $this->db->query("select * from menu");		
               return $query->result();
    }
    
    function active_menu() {
       $menu_id = $_POST['activemenu'];      
       $menu = substr($menu_id,1,3);
       $active = substr($menu_id,0,1);

        if ($active == "1") {
            $data = array(
                'active' => '0'
            );
        } else {
            $data = array(
                'active' => '1'
            );
        }

        $this->db->where('menu_id',  $menu);
        $this->db->update('menu', $data);  
    }
    
    function delete_menu() {      
         $menu_id = $_POST['deletemenu'];
        
        $sql = "delete from chk_menu_group ";
        $sql .= "where menu_id = '$menu_id'";
        
        $this->db->query($sql);
        
        $this->db->where('menu_id', $menu_id);
        $this->db->delete('menu');
    }
    
    function new_menu() {  
        
        $menuidmax = $_POST['menuidmax']+1;
        $menu_name = $_POST['fname'];
        $menu_link = $_POST['fname2'];
        $active = 0;
        
        $data = array(
            'menu_id' => $menuidmax,
            'menu_name' => $menu_name,
            'menu_link' => $menu_link,
            'active' => $active,
        );
        $this->db->insert('menu', $data);
        
        // sql insert chk_menu_group
        for($i=0; $i<sizeof($_POST['gmenu_id']); $i++)
        {
            $gmenu_id[$i] = $_POST['gmenu_id'][$i];
            $data = array(
                'cmg_id' => '',
                'menu_id' => $menuidmax,
                'gmenu_id' => $gmenu_id[$i],
               
            );
            $this->db->insert('chk_menu_group', $data);
        }
                         
    }
    
     function select_menuid_max(){
      $this->db->select_max('menu_id');
        $query = $this->db->get('menu');
        return $query->result();        
    }  
     
    
    function edit_menu() {
       $menu_id=  $_POST['editmenu']; 
       $query = $this->db->query("select * from menu where menu_id = '$menu_id'");		
       return $query->result();
    }
   
    function chk_group_menu() {
        $menuid = $_POST['editmenu'];
        $sql = "SELECT b.menu_id,a.menu_name,b.gmenu_id,c.gmenu_name
                FROM menu a, chk_menu_group b, group_menu c
                WHERE b.menu_id = $menuid";
        $data = $this->db->query($sql);
        return $data->result();        
    }
    
    function edit_menu2() {  
       $menuid = $_POST['menuid'];
       $gmenuid = $_POST['gmenu_id'];
       $this->menu_name = $_POST['fname'];
       $this->menu_link = $_POST['fname2'];
       $this->active = 0;
       $this->db->update('menu', $this, array('menu_id' => $menuid));  
      
        
        //clear
        $this->db->where('menu_id',$menuid);
        $this->db->delete('chk_menu_group');
        
        for($i=0; $i<sizeof($gmenuid); $i++)
        {
            $gmenu_id[$i] =  $gmenuid[$i];
            $data = array(
                'cmg_id'=>'',
                'menu_id' =>  $menuid,
                'gmenu_id' => $gmenu_id[$i]
            );           
            $this->db->insert('chk_menu_group', $data);
        }  
        
    }
//---------------------------------------------------------------------
// start group menu manager    
    
    function select_gmenu() {
       $query = $this->db->get('group_menu');		
       return $query->result();
    }
    
    function active_gmenu() {
       $gmenu_id = $_POST['activegmenu'];         
       $gmenu = substr($gmenu_id,1,3);
       $active = substr($gmenu_id,0,1);

        if ($active == "1") {
            $data = array(
                'active' => '0'
            );
        } else {
            $data = array(
                'active' => '1'
            );
        }

        $this->db->where('gmenu_id',  $gmenu);
        $this->db->update('group_menu', $data);  
    }
    
    function delete_gmenu() {      
        $gmenu_id = $_POST['deletegmenu'];
        $this->db->where('gmenu_id',$gmenu_id);
        $this->db->delete('group_menu');           
    }
    
    function new_gmenu() {    
        $this->gmenu_id = '';
        $this->gmenu_name = $_POST['gname'];
        $this->position= $_POST['gname2'];
        $this->active= 0;
        $this->db->insert('group_menu', $this);                              
    }
        
    function edit_gmenu() {
        $gmenu_id=  $_POST['editgmenu'];
        $query = $this->db->query("select * from group_menu where gmenu_id = '$gmenu_id'");		
        return $query->result();
    }
    
     function edit_gmenu2() {   
        $this->gmenu_name = $_POST['gname'];
        $this->position = $_POST['gname2'];
        $this->active = 0;
        $this->db->update('group_menu', $this, array('gmenu_id' => $_POST['gmenuid']));      
    }
     
}

?>
